package org.donhuan.rms.ui.importWizards;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.donghuan.rms.framework.domain.Archive;
import org.donhuan.rms.ui.model.Project;
import org.donhuan.rms.ui.model.Session;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

public class NewProjectCreationWizardPage extends WizardPage {
	private static class ContentProvider implements IStructuredContentProvider {
		public Object[] getElements(Object inputElement) {
			List<Archive> value=null;
			if (inputElement instanceof Project)
				value=((Project) inputElement).getArchives();
			if(value!=null)ArrayUtils.nullToEmpty(value.toArray());
				return new Object[0];
		}

		public void dispose() {
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}

	private class TableLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			String fieldName=element.getClass().getDeclaredFields()[columnIndex].getName();
			try {
				return FieldUtils.readDeclaredField(element, fieldName, true).toString();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}return "";
		}
	}

	private Table table;
	private Session session;
	private TableViewer tableViewer;
	private Project project;
	private Combo combo;
	private String[] items = new String[0];

	public String[] getItems() {
		return items;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	public NewProjectCreationWizardPage(String pageName) {
		super(pageName);
		setTitle(pageName);
		session = Session.getInstance();
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.V_SCROLL);
		setControl(container);
		container.setLayout(new GridLayout(3, false));

		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setText("New Label");

		combo = new Combo(container, SWT.NONE);

		GridData gd_combo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1,
				1);
		gd_combo.widthHint = 339;
		combo.setLayoutData(gd_combo);
		for (int i = 0; i < Session.getInstance().getParents().size(); i++) {
			String element = Session.getInstance().getParents().get(i)
					.getProjectName();
			items = (String[]) ArrayUtils.add(items, element);
		}
		combo.setItems(items);
		Button btnNewButton = new Button(container, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				combo.add(combo.getText());
				Session.getInstance().getParents()
						.add(new Project(combo.getText()));
			}
		});
		btnNewButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		btnNewButton.setText("New Button");

		Composite composite = new Composite(container, SWT.NONE);
		GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, false, false,
				2, 8);
		gd_composite.heightHint = 260;
		composite.setLayoutData(gd_composite);
		TableColumnLayout tcl_composite = new TableColumnLayout();
		composite.setLayout(tcl_composite);

		tableViewer = new TableViewer(composite, SWT.BORDER | SWT.CHECK
				| SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tcl_composite.setColumnData(tblclmnNewColumn, new ColumnPixelData(150,
				true, true));
		tblclmnNewColumn.setText("New Column");

		Button btnNewButton_1 = new Button(container, SWT.NONE);
		btnNewButton_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		btnNewButton_1.setText("New Button");

		Button btnNewButton_2 = new Button(container, SWT.NONE);
		btnNewButton_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		btnNewButton_2.setText("New Button");

		Button btnNewButton_3 = new Button(container, SWT.NONE);
		btnNewButton_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		btnNewButton_3.setText("New Button");

		Button btnNewButton_4 = new Button(container, SWT.NONE);
		btnNewButton_4.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		btnNewButton_4.setText("New Button");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		Group grpDescription = new Group(container, SWT.NONE);

		GridData gd_grpDescription = new GridData(SWT.FILL, SWT.FILL, true,
				true, 3, 1);
		gd_grpDescription.heightHint = 52;
		grpDescription.setLayoutData(gd_grpDescription);
		grpDescription.setText("Description");

	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}
}
